<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/common/header.jsp"%>
<html>
<body class="easyui-layout">
	<div region="west" style="width:180px;" id="west" title="系统菜单树">
		<div id="ztreeObj" class="ztree"></div>
	</div>
	<div region="center" title="菜单操作">
		<form id="menuForm" name="menuForm">
			<!-- 自定义标签工具栏 -->
			<jiepos:buttonOperateTag menuCode="${menuCode}"></jiepos:buttonOperateTag>
			<!-- 表格 -->
			<table id="listtable"></table>
		</form>
	</div>
</body>
<script type="text/javascript">
	var muuid = grid = null;
	$(function(){
		var ztreeObj;
		var setting = {
			data:{
				key: {name: "MNAME"},
				simpleData:{enable: true, idKey: "MID", pIdKey: "PID"}
			},
			callback:{
				onClick: loadBtn
			}
		}
		/*请求菜单树形*/
		$.ajax({
			url: "getMenuTree.do",
			success: function(result){
				ztreeObj = $.fn.zTree.init($("#ztreeObj"), setting, result);
			}
		});
		
		/*加载表格数据*/
		grid = $("#listtable").jpgrid({
			url: "getMenubtnListJson.do",
			columns: [
				{title: "按钮代码", field: "BUTTON_CODE", width: 160},
				{title: "按钮名称", field: "BUTTON_NAME", width: 80},
				{title: "按钮类型", field: "BUTTON_TYPE", width: 100},
				{title: "函数名称", field: "FUNCTION_NAME", width: 120},
				{title: "按钮排序", field: "BUTTON_ORDERS", width: 80},
				{title: "按钮链接", field: "BUTTON_URL", width: 300},
				{title: "按钮图片", field: "BUTTON_ICONCLS", width: 80},
				{title: "记录后显示按钮", field: "IS_SHOW_LIST", width: 100}
			],
			onBeforeLoad: function(){
				if(!muuid){
					return false;// 发送加载数据的请求前触发，如果返回 false 加载动作就会取消
				}
			}
		});
	});
	
	/*点击菜单加载按钮*/
	function loadBtn(event, treeId, treeNode){
		if(treeNode.MENU_URL){
			muuid = treeNode.MID;
			grid.clearChecked();// 清空之前选择的行
			grid.load({muuid: muuid});
		}
	}
	
	/*增加按钮事件*/
	function addBtn(url){
		if(!muuid){
			$.jpmessage.warning("请先选择需要添加按钮的菜单!");
			return;
		}
		openModal(null, {href: url + muuid, width: 450}, callback);
	}
	
	/*修改按钮*/
	function modifyBtn(url){
		var checkeds = grid.getChecked();
		if(!checkeds || checkeds.length == 0){
			$.jpmessage.warning("请至少选择一条记录!");
		}
		if(checkeds.length > 1){
			$.jpmessage.warning("只能选择一条记录进行修改!");
			return;
		}
		openModal(null, {href: url + muuid + "&button_uuid=" + checkeds[0].UUID, width: 450}, callback);
	}
	
	/*删除按钮*/
	function deleteBtn(url){
		commonDel(url, "delete", "listtable");
	}
	
	/*确定按钮回调函数*/
	function callback(){
		if(!$("#btnForm").form("validate")){
			return false;
		};
		$("#btnForm").form("submit", {
			url: "saveBtn.do",
			success: function(data){
				data = eval('(' + data + ')');
				if(data.isOk === true){
					$.jpmessage.info("操作成功!");
					$("#btnForm").form("reset");
					grid.reload();
// 					window.location.reload();
				}else{
					$.jpmessage.error("操作失败!");
				}
			}
		});
	}
</script>
</html>
